package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;


    String imgName ;

    /**
     * 文件上传
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取原始名字 并且截取获得文件 后缀
        String originalFilename = imgFile.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i);
        //生成 随机的名字 拼接上后缀
        imgName = UUID.randomUUID().toString()+substring;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),imgName);
            //吧上传到 七牛云的 图片名字存到 redis里面
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
        } catch (IOException e) {
            return  new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return  new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,imgName);
    }

    /**
     * 添加方法
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        try {
            setMealService.add(checkgroupIds,setmeal);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imgName);
          return   new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
          return   new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.findPage(queryPageBean);

    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {

        Setmeal setmeal =  setMealService.findById(id);
        if (setmeal != null) {

            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }
        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }


    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup() {

        try {
            List<CheckGroup>  list = setMealService.findAllCheckGroup();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findContactBySetMealId")
    public Result findContactBySetMealId(Integer setMealId) {

        List<Integer> checkGroupId =  setMealService.findContactBySetMealId(setMealId);
        if (checkGroupId != null) {

            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupId);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    @RequestMapping("/edit")
    public Result edit(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        try {
            setMealService.edit(checkgroupIds,setmeal);
            return   new Result(true,MessageConstant.Edit_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return   new Result(false,MessageConstant.Edit_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            setMealService.delete(id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

}
